package org.dromara.wms.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.web.core.BaseService;
import org.dromara.wms.domain.WmsStockCheckLine;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wms.domain.WmsStockCheck;
import org.dromara.wms.service.WmsStockCheckService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 库存盘点
 *
 * @author wenrui Su
 * @date 2024-05-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/stockCheck")
public class WmsStockCheckController extends BaseService {

  private final WmsStockCheckService wmsStockCheckService;

/**
 * 查询库存盘点列表
 */
@SaCheckPermission("wms:stockCheck:list")
@GetMapping("/list")
  public TableDataInfo<WmsStockCheck> list(WmsStockCheck bo, PageQuery pageQuery) {
    return wmsStockCheckService.queryPageList(bo, pageQuery);
  }

  /**
   * 导出库存盘点列表
   */
  @SaCheckPermission("wms:stockCheck:export")
  @Log(title = "库存盘点", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(WmsStockCheck bo, HttpServletResponse response) {
    List<WmsStockCheck> list = wmsStockCheckService.queryList(bo);
    ExcelUtil.exportExcel(list, "库存盘点", WmsStockCheck. class,response);
  }

  /**
   * 获取库存盘点详细信息
   *
   * @param id 主键
   */
  @SaCheckPermission("wms:stockCheck:query")
  @GetMapping("/{id}")
  public R<WmsStockCheck> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
    return R.ok(wmsStockCheckService.queryById(id));
  }

  /**
   * 新增库存盘点
   */
  @SaCheckPermission("wms:stockCheck:add")
  @Log(title = "库存盘点", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody WmsStockCheck bo) {
    return toAjax(wmsStockCheckService.insertByBo(bo));
  }

  /**
   * 修改库存盘点
   */
  @SaCheckPermission("wms:stockCheck:edit")
  @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody WmsStockCheck bo) {
    return toAjax(wmsStockCheckService.updateByBo(bo));
  }
  /**
   * 查询库存盘点子列表
   */
  @SaCheckPermission("wms:stockCheck:lineList")
  @GetMapping("/lineList")
  public TableDataInfo<WmsStockCheckLine> lineList(WmsStockCheckLine bo, PageQuery pageQuery) {
    return wmsStockCheckService.queryPageLineList(bo, pageQuery);
  }

  /**
   * 删除库存子表数据
   */
  @SaCheckPermission("wms:stockCheck:list")
  @DeleteMapping("/delLineList/{lineId}")
  public Boolean delLineList(@NotNull(message = "主键不能为空") @PathVariable Long lineId) {
    return wmsStockCheckService.delLineList(lineId);
  }
  /**
   * 库存盘点 发送
   */
  @SaCheckPermission("wms:stockCheck:send")
  @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/send")
  public R<Void> send(@Validated(EditGroup.class) @RequestBody WmsStockCheck bo) {
    return toAjax(wmsStockCheckService.sendUpdate(bo));
  }
  /**
   * 库存盘点 执行
   */
  @SaCheckPermission("wms:stockCheck:setStock")
  @Log(title = "库存盘点", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/setStock")
  public R<Void> setStock(@Validated(EditGroup.class) @RequestBody WmsStockCheck bo) {
    return toAjax(wmsStockCheckService.setUpdateByBo(bo));
  }


  /**
   * 删除库存盘点
   *
   * @param ids 主键串
   */
  @SaCheckPermission("wms:stockCheck:remove")
  @Log(title = "库存盘点", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空")
                        @PathVariable Long[] ids) {
    return toAjax(wmsStockCheckService.deleteWithValidByIds(List.of(ids), true));
  }
}
